// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.population;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import zombie.core.Rand;

@XmlRootElement
public class ItemManager {
    public ArrayList<CarriedItem> m_Items = new ArrayList();
    @XmlTransient
    public static ItemManager instance;

    public static void init() {
        File file = zombie.ZomboidFileSystem.instance.getMediaFile("items" + File.separator + "items.xml");
        instance = Parse(file.getPath());
    }

    public CarriedItem GetRandomItem() {
        int _int = Rand.Next(this.m_Items.size() + 1);
        return _int < this.m_Items.size() ? (CarriedItem)this.m_Items.get(_int) : null;
    }

    public static ItemManager Parse(String filename) {
        try {
            return parse(filename);
        } catch (JAXBException jAXBException) {
            jAXBException.printStackTrace();
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }

        return null;
    }

    public static ItemManager parse(String filename) throws JAXBException, IOException {
        FileInputStream fileInputStream = new FileInputStream(filename);

        ItemManager itemManager0;
        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(ItemManager.class);
            Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
            ItemManager itemManager1 = (ItemManager)unmarshaller.unmarshal(fileInputStream);
            itemManager0 = itemManager1;
        } catch (Throwable throwable0) {
            try {
                fileInputStream.close();
            } catch (Throwable throwable1) {
                throwable0.addSuppressed(throwable1);
            }

            throw throwable0;
        }

        fileInputStream.close();
        return itemManager0;
    }

    public static void Write(ItemManager out, String filename) {
        try {
            write(out, filename);
        } catch (JAXBException jAXBException) {
            jAXBException.printStackTrace();
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    public static void write(ItemManager out, String filename) throws IOException, JAXBException {
        FileOutputStream fileOutputStream = new FileOutputStream(filename);

        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(ItemManager.class);
            Marshaller marshaller = jAXBContext.createMarshaller();
            marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
            marshaller.marshal(out, fileOutputStream);
        } catch (Throwable throwable0) {
            try {
                fileOutputStream.close();
            } catch (Throwable throwable1) {
                throwable0.addSuppressed(throwable1);
            }

            throw throwable0;
        }

        fileOutputStream.close();
    }
}
